﻿using System;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Models;
using Home.MobileSales.Sales.Interface;
using System.Windows.Forms;
using Home.MobileSales.WorkGps;

namespace Home.MobileSales.Sales.Presenter
{
    public class SettingsPresenter 
    {
        private ISettingsView _view;
        private Settings settings;

        public SettingsPresenter(ISettingsView view)
        {
            settings = Settings.GetInstance();
            _view = view;
            loadSettingsInView();
            _view.SaveChanges += SaveSettings;
            _view.CancelChanges += CancelChanges;
            _view.TestGps += TestGps;
        }

        private void loadSettingsInView()
        {
            _view.CompanyName = settings.CompanyName;
            _view.AgentName = settings.AgentName;

            _view.DbPassword = string.IsNullOrEmpty(settings.DatabasePassword) ? string.Empty : settings.DatabasePassword;
            _view.DbPath = string.IsNullOrEmpty(settings.DatabasePath) ? string.Empty : settings.DatabasePath;
            _view.ExportPath = string.IsNullOrEmpty(settings.ExportPath) ? string.Empty : settings.ExportPath;
            _view.ImportPath = string.IsNullOrEmpty(settings.ImportPath) ? string.Empty : settings.ImportPath;
            _view.IsSaveFile = settings.IsSaveFiles == null ? false : settings.IsSaveFiles;
            _view.CheckRemainingGoods = settings.CheckRemainingGoods;
            _view.VisibleNullGoods = settings.VisibleNullGoods;
            _view.IsVanSales = settings.IsVanSales;
            
            _view.FtpUrl = string.IsNullOrEmpty(settings.FtpUrl) ? string.Empty : settings.FtpUrl;
            _view.FtpPort = settings.FtpPort == null ? 21 : settings.FtpPort;
            _view.FtpUser = string.IsNullOrEmpty(settings.FtpUrl) ? string.Empty : settings.FtpLogin;
            _view.FtpPassword = string.IsNullOrEmpty(settings.FtpUrl) ? string.Empty : settings.FtpPassword;
            _view.ftpIsPasiveMode = settings.IsPassivMode == null ? false : settings.IsPassivMode;
            _view.FtpImportPath = string.IsNullOrEmpty(settings.FtpImportPath) ? string.Empty : settings.FtpImportPath;
            _view.FtpExportPath = string.IsNullOrEmpty(settings.FtpExportPath) ? string.Empty : settings.FtpExportPath;

            _view.sklads = DataManager.GetInstance().Sklads.GetSklads();
            _view.CurrentSklad = settings.DefaultSklad;
            _view.AutoGenerateNumber = settings.AutoGenerateNumber;
            _view.NumberDocument = settings.NumberDocument;

            _view.EnableGps = settings.EnableGPS;

            _view.PrintPath = settings.PrintPath;
            _view.ExecuteProgramPrint = settings.ExecuteProgramPrint;

          
        }

        public void SaveSettings(object sender, EventArgs e)
        {
            try
            {
                settings.AgentName = _view.AgentName;
                settings.CompanyName = _view.CompanyName;

                settings.DatabasePath = _view.DbPath;
                settings.DatabasePassword = _view.DbPassword;
                settings.ImportPath = _view.ImportPath;
                settings.ExportPath = _view.ExportPath;
                settings.IsSaveFiles = _view.IsSaveFile;
                settings.VisibleNullGoods = _view.VisibleNullGoods;
                settings.IsVanSales = _view.IsVanSales;

                settings.FtpUrl = _view.FtpUrl;
                settings.FtpPort = _view.FtpPort;
                settings.FtpLogin = _view.FtpUser;
                settings.FtpPassword = _view.FtpPassword;
                settings.IsPassivMode = _view.ftpIsPasiveMode;
                settings.FtpImportPath = _view.FtpImportPath;
                settings.FtpExportPath = _view.FtpExportPath;

                settings.DefaultSklad = _view.CurrentSklad;
                settings.AutoGenerateNumber = _view.AutoGenerateNumber;
                settings.NumberDocument = _view.NumberDocument;
                settings.EnableGPS = _view.EnableGps;

                settings.PrintPath = _view.PrintPath;
                settings.ExecuteProgramPrint = _view.ExecuteProgramPrint;

                settings.Save();
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);}
        }

        public void CancelChanges (object sender, EventArgs e)
        {
            settings = Settings.GetInstance();
        }

        public void TestGps(object sender, EventArgs e)
        {
            GpsWorker.GetInstance().StartGps();
            _view.GpsTestData = GpsWorker.GetInstance().GpsData.ToString();
        }
    }
}
